<?php

class Router {

    public $parsed;

    public $routes = array(
      "default_get" => array(
//          'url' => "/:controller/:id",
//          'params' => array('action'=> 'show'),
//          'requirements' => array(
//              'method' => 'get',
//              'params' => array('id'=> '/d+/')
//            ),
//
//          'method'=> 'GET',
          'patern' =>  "/GET\s\/(?P<_controller>\w+)\/(?P<id>\d+)/",
          "fixos" => array('_action'=> 'show'),
          "dinamicos" => array("_controller",  "id")
        ),

      "default_with_id" => array('patern' =>  "/[GET|PUT|POST|DELETE]\s\/(?P<_controller>\w+)\/(?P<_action>\w+)\/(?P<id>\d+)/", "fixos" => array(), "dinamicos" => array("_controller", "_action", "id")),
      "controller_action" => array('patern' =>  "/[GET|PUT|POST|DELETE]\s\/(?P<_controller>\w+)\/(?P<_action>\w+)/", "fixos" => array(), "dinamicos" => array("_controller", "_action")),
      "default_index" => array('patern' =>  "/[GET|PUT|POST|DELETE]\s\/(?P<_controller>\w+)/", "fixos" => array('_action'=> "index"), "dinamicos" => array("_controller")),
      "home" => array('patern' =>  "/[GET|PUT|POST]\s\/.*/", "fixos" => array('_controller'=> "teste", '_action'=> 'index'), "dinamicos" => array())
    );


    public function  __construct()
    {

    }

    public function parse($pathInfo = "/", $method='GET')
    {
      foreach($this->routes as $key => $route)
      {
        if(preg_match($route['patern'], $method." ".$pathInfo, $matches))
        {
          #echo($method." ".$pathInfo."<br>");
          #echo("$key<br>");
          $params = $route['fixos'];
          foreach($route['dinamicos'] as $key)
          {
            $params[$key] = $matches[$key];
          }
          $this->parsed = $params;

          break;
        }

      }
    }
}
?>
